export default (sequelize, DataTypes) => {
  const User = sequelize.define(
    'User',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
        comment: '用户唯一标识',
      },
      username: {
        type: DataTypes.STRING(50),
        allowNull: false,
        unique: {
          name: 'username',
          msg: '用户名已存在',
        },
        validate: {
          len: {
            args: [3, 20],
            msg: '用户名长度必须在3-20个字符之间',
          },
          isAlphanumeric: {
            msg: '用户名只能包含字母和数字',
          },
        },
        comment: '用户名',
      },
      passwordHash: {
        type: DataTypes.STRING(255),
        allowNull: false,
        field: 'password_hash',
        comment: '密码哈希值',
      },
      avatarUrl: {
        type: DataTypes.STRING(500),
        allowNull: true,
        field: 'avatar_url',
        validate: {
          isUrl: {
            msg: '头像URL格式不正确',
          },
        },
        comment: '用户头像URL',
      },
      nickname: {
        type: DataTypes.STRING(50),
        allowNull: true,
        validate: {
          len: {
            args: [1, 50],
            msg: '昵称长度必须在1-50个字符之间',
          },
        },
        comment: '用户昵称',
      },
      gender: {
        type: DataTypes.ENUM('male', 'female', 'other'),
        allowNull: true,
        comment: '性别：male-男，female-女，other-其他',
      },
      phone: {
        type: DataTypes.STRING(20),
        allowNull: true,
        unique: {
          name: 'phone',
          msg: '手机号已存在',
        },
        validate: {
          is: {
            args: /^1[3-9]\d{9}$/,
            msg: '手机号格式不正确',
          },
        },
        comment: '手机号',
      },
      email: {
        type: DataTypes.STRING(100),
        allowNull: true,
        unique: {
          name: 'email',
          msg: '邮箱已存在',
        },
        validate: {
          isEmail: {
            msg: '邮箱格式不正确',
          },
        },
        comment: '邮箱地址',
      },
      bio: {
        type: DataTypes.STRING(500),
        allowNull: true,
        validate: {
          len: {
            args: [0, 500],
            msg: '个人简介不能超过500个字符',
          },
        },
        comment: '个人简介',
      },
      gradeLevel: {
        type: DataTypes.ENUM('primary', 'middle', 'high'),
        allowNull: false,
        field: 'grade_level',
        comment: '年级层次：primary-小学，middle-初中，high-高中',
      },
      lastLoginAt: {
        type: DataTypes.DATE,
        allowNull: true,
        field: 'last_login_at',
        comment: '最后登录时间',
      },
    },
    {
      tableName: 'users',
      timestamps: true,
      underscored: true,
      indexes: [
        {
          name: 'idx_username',
          fields: ['username'],
        },
        {
          name: 'idx_grade_level',
          fields: ['grade_level'],
        },
        {
          name: 'idx_created_at',
          fields: ['created_at'],
        },
      ],
      comment: '用户表',
    }
  );

  // 定义关联关系
  User.associate = (models) => {
    User.hasMany(models.Essay, {
      foreignKey: 'userId',
      as: 'essays',
      onDelete: 'CASCADE',
    });
  };

  // 实例方法：转换为安全的JSON（不包含密码）
  User.prototype.toSafeJSON = function () {
    const values = { ...this.get() };
    delete values.passwordHash;
    return values;
  };

  return User;
};

